<template>
  <el-dialog title="产品BOM选择" v-if="showFlag" :visible.sync="showFlag" :modal="false" width="50%" center>
    <div class="info-container">
      <div class="info-item">
        <span>产品名称:</span>
        <span class="content">{{ planning.productName }}</span>
      </div>
      <div class="info-item">
        <span>产品编号:</span>
        <span class="content">{{ planning.productCode }}</span>
      </div>
      <div class="info-item">
        <span>规格型号:</span>
        <span class="content">{{ planning.productSpc }}</span>
      </div>
      <div class="info-item">
        <span>单位:</span>
        <span class="content">{{ planning.unitOfMeasure }}</span>
      </div>
    </div>
    <el-table v-loading="loading" :data="bomList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center">
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.bomId" @change="handleRowChange(scope.row)">
            {{ '' }}
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="bom编号" align="center" prop="bomCode" />
      <el-table-column label="版本号" align="center" prop="bomName" />
      <!--      <el-table-column label="默认bom" align="center" prop="bomDefault">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.bom_default" :value="scope.row.bomDefault" />
              </template>
            </el-table-column>
            >-->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBomGl } from '@/api/mes/md/bomgl';

export default {
  name: 'BomSelectSingle',
  props: {
    planning: undefined
  },
  dicts: ['bom_default'],
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: this.planning.productId,
        status: '发布',
      }
    };
  },

  watch: {
    'planning.productId': function (newProductId, oldProductId) {
      if (newProductId !== oldProductId) {
        this.queryParams.itemId = newProductId;
        this.getList();
      }
    }
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询产品BOM关系列表 */
    getList() {
      this.loading = true;
      listBomGl(this.queryParams).then(response => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit('onSelected', this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },

    //确定选中
    confirmSelect() {
      if (this.selectedItemId == null || this.selectedItemId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    }
  }
};
</script>
<style scoped>
.info-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.info-item {
  width: 25%; /* Adjust width as needed */
  margin-bottom: 10px;
}

.info-item span {
  margin-left: 10px;
}

.info-item span:first-child {
  font-family: '苹方 常规', '苹方', sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 14px;
  color: #262626;
  text-align: left;
  line-height: 20px;
  display: inline-block;
}

.content {
  color: #8c8c8c;
  text-align: left;
}
</style>
